package renda.action.user;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import renda.common.SessionUtil;
import renda.dao.serivce.TopicChildService;
import renda.dao.serivce.TopicsService;
import renda.pojo.Topicchild;
import renda.pojo.Topics;

public class TopicsAction extends ActionSupport {

	private static final long serialVersionUID = 6755305025435784376L;

	// 查询使用的对象
	private Topics search = new Topics();
	// 保存使用的对象
	private Topics topics;
	private int page = 1;
	private int size = 10;

	private TopicsService topicsService;

	private TopicChildService topicChildService;

	public String list() {
		// TODO userId获取当前用户，如果是管理员，另当别论
		list2();
		return "success";
	}

	private void list2() {
		page = (page - 1) * size;
		List<Topics> topicses = topicsService.listTopics(search.getTopicname(), SessionUtil.getCurrentUsers().getUid(),
				page, size);
		if (topicses != null && topicses.size() > 0) {
			for (Topics topics : topicses) {
				if (topics.getTopicname().length() > 20) {
					topics.setTopicname(topics.getTopicname().substring(0, 20) + "...");
				}
			}
		}
		ServletActionContext.getRequest().setAttribute("topics", topicses);
	}

	public String save() {
		// TODO userId获取当前用户，如果是管理员，另当别论
		topics.setTopicname(topics.getTopicname().trim());
		topics.setTopiccontent(topics.getTopiccontent().trim());
		topics.setIsuserreturn(1);// 初始时为1
		topics.setIsadminreturn(0);// 初始时为0
		topics.setTopictime(new Date());
		topics = topicsService.saveTopics(topics);
		Topicchild topicchild = new Topicchild();
		topicchild.setContent(topics.getTopiccontent());
		topicchild.setTopicid(topics.getTid());
		topicchild.setReturntime(new Date());
		topicchild.setWriter(SessionUtil.getCurrentUsers().getUname());
		topicChildService.saveTopicchild(topicchild);
		list2();
		return "success";
	}

	public void validateSave() {
		list2();
		if (topics != null) {
			boolean isError = false;
			if (StringUtils.isBlank(topics.getTopicname())) {
				isError = true;
				ServletActionContext.getRequest().setAttribute("topicnameerror", "意见标题不能为空");
				this.addFieldError("topicnameerror", "意见标题不能为空");
			} else if (topics.getTopicname().trim().length() > 100) {
				isError = true;
				ServletActionContext.getRequest().setAttribute("topicnameerror", "意见标题长度最大为100个字符");
				this.addFieldError("topicnameerror", "意见标题长度最大为100个字符");
			} else if (StringUtils.isBlank(topics.getTopiccontent())) {
				isError = true;
				ServletActionContext.getRequest().setAttribute("topiccontenterror", "意见建议不能为空");
				this.addFieldError("topiccontenterror", "意见建议不能为空");
			} else if (topics.getTopiccontent().trim().length() > 4000) {
				isError = true;
				ServletActionContext.getRequest().setAttribute("topiccontenterror", "意见建议长度最大为4000个字符");
				this.addFieldError("topiccontenterror", "意见建议长度最大为4000个字符");
			}
			if (isError) {
				ServletActionContext.getRequest().setAttribute("topic", topics);
			}
		}
	}

	public TopicsService getTopicsService() {
		return topicsService;
	}

	public void setTopicsService(TopicsService topicsService) {
		this.topicsService = topicsService;
	}

	public Topics getTopics() {
		return topics;
	}

	public void setTopics(Topics topics) {
		this.topics = topics;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public Topics getSearch() {
		return search;
	}

	public void setSearch(Topics search) {
		this.search = search;
	}

	public TopicChildService getTopicChildService() {
		return topicChildService;
	}

	public void setTopicChildService(TopicChildService topicChildService) {
		this.topicChildService = topicChildService;
	}

}
